<%@ page import="java.io.File" %>
<%@ page import="org.apache.commons.io.FilenameUtils" %>
<%@ page import="IPADistribution.Constant.Constant" %>
<%@ page import="java.net.URLEncoder" %><%--
  Created by IntelliJ IDEA.
  User: guangyu
  Date: 2017/11/1
  Time: 下午6:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>
    <title>plist list</title>

</head>
<body>

<a href="ca.crt">download crt</a><br>

<%
    String host = request.getHeader("host");
    String filePath = request.getParameter(Constant.param_filePath);
    String filesPath = request.getServletContext().getRealPath(Constant.files_path);
    if (filePath == null) {
        filePath = "";
    }

    String realPath = filesPath+File.separator+filePath;
    //获取该路径下所有文件或者文件夹
    File file = new File(realPath);
    if (file.exists()) {

        File[] files = file.listFiles();
        for (File f : files) {
            String href = null;
            //相对路径
            String relativePath = f.getAbsolutePath().replace(filesPath+File.separator, "");
            if (f.isDirectory()) {
                //a标签跳转该jsp
                href = "/plistlist.jsp?filePath="+relativePath;
            }else  {
                //a标签下载文件
                if (relativePath.endsWith(".plist")){
                    String hostIp = host.substring(0,host.indexOf(':'));
                    System.out.println("ipAddress="+hostIp);
                    href = "itms-services://?action=download-manifest&url=https://"+hostIp+":8443/"+
                            Constant.files_path+File.separator+URLEncoder.encode(relativePath, "UTF-8");

                }else  {
                    href = relativePath;
                }
            }
            out.write("<a href=\""+href+"\">"+ FilenameUtils.getName(relativePath)+"</a>"+"<br>"+"\n");
        }
    }
%>

</body>
</html>
